# -*-coding:utf-8 -*-

import subprocess



'''
stdout=subprocess.DEVNULL,  # 隐藏标准输出  
stderr=subprocess.DEVNULL   # 隐藏标准错误输出
'''

'''
批量测试crate能否成功cargo build
'''
def cargo_build_batch(crate_path):
    try:
        result = subprocess.run(
            ['cargo', 'build'],
            cwd=crate_path,
            check=True,
            # stdout=subprocess.DEVNULL,
            # stderr=subprocess.DEVNULL,
            timeout=180  # 设置超时时间为 180 秒
        )
        return True
    except subprocess.TimeoutExpired:
        print(f"{crate_path} Time Out!!!")
        return False
    except subprocess.CalledProcessError:
        return False


'''
批量测试crate能否成功cargo build
'''
def cargo_test_batch(crate_path):
    try:
        result = subprocess.run(
            ['cargo', 'test'],
            cwd=crate_path,
            check=True,
            # stdout=subprocess.DEVNULL,
            # stderr=subprocess.DEVNULL,
            timeout=120  # 设置超时时间为 120 秒
        )
        return True
    except subprocess.TimeoutExpired:
        print(f"{crate_path} Time Out!!!")
        return False
    except subprocess.CalledProcessError:
        return False


# def llvmcov_crate(crate_path, output_file):
#     string_split = '--------------------------------------------------------------------------------------'
#     with open(output_file, 'w') as f:  # 以写模式打开文件
#         f.write(string_split + '\n')
#         try:
#             # 执行 cargo llvm-cov，并设置最大运行时间为 2 分钟
#             result = subprocess.run(
#                 ['cargo', 'llvm-cov'],
#                 cwd=crate_path,
#                 check=True,
#                 stdout=f,  # 将标准输出重定向到文件
#                 stderr=subprocess.STDOUT,  # 将标准错误也重定向到同一个文件
#                 timeout=120  # 设置超时时间为 120 秒
#             )
#             return True  # 如果构建成功
#         except subprocess.TimeoutExpired:
#             f.write('\nOperation timed out after 2 minutes.\n')
#             return False  # 超时
#         except subprocess.CalledProcessError:
#             f.write('\nBuild failed.\n')
#             return False  # 如果构建失败